from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions

# 无头浏览器设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# 规避检查
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])

bro = webdriver.Chrome(executable_path="./chromedriver.exe", chrome_options=chrome_options, options=options)

# ============================= #
#           爬取车品牌
# ============================= #

# bro.get("https://price.pcauto.com.cn")
#
# for a in bro.find_element_by_id("tree").find_elements_by_class_name("ppLink"):
#     print(a.get_attribute("href"), ",", a.text)
#     with open("data/brand.txt", "a", encoding="utf-8") as file:
#         file.write(a.get_attribute("href") + "," + a.text + "\n")

# ============================= #
#           爬取车系
# ============================= #

with open("data/brand.txt", "r", encoding="utf-8") as lines:
    for line in lines:
        print(line.split(",")[0])
        bro.get(line.split(",")[0])

        subTable = bro.find_element_by_id("tree").find_element_by_id(
            "subTable" + line.split(",")[0].split("nb")[1].split("/")[0])
        for a in subTable.find_elements_by_css_selector(".product a"):
            print(a.get_attribute("href").split("/")[-2], a.get_attribute("title"))
            with open("data/series.txt", "a", encoding="utf-8") as file:
                file.write(a.get_attribute("href").split("/")[-2] + "," + a.get_attribute("title") + "\n")

# for a in bro.find_element_by_id("tree").find_elements_by_class_name("ppLink"):
#     print(a.get_attribute("href"), ",", a.text)
